package com.jiazhong.Object类详解.eaquals方法;

import java.util.Objects;

//==是一个比较运算符
//1.既可以判断基本类型，又可以判断引用类型（地址）
//2.如果判断基本类型，判断的值是否相等
//3.如果判断引用类型，判断的地址是否相等，即判定是不是同一个对象
//equals
//1.equals只能判断引用类型
//2.默认判断地址是否相等，子类中往往重写该方法，用于判断内容是否相等
public class Test01 {
    public static void main(String[] args) {
        Person person1=new Person("jack",10,'男');
        Person person2=new Person("jack",20,'男');
        System.out.println(person1.equals(person2));
    }
}

class Person {
    private String name;
    private int age;
    private char gender;
    //重写Object类的equals方法(判断内容是否相等)

    @Override
    public boolean equals(Object o) {
        if (this == o)
            return true;
        if (o instanceof Person){
            Person person = (Person) o;
            return age == person.age && gender == person.gender && Objects.equals(name, person.name);
        }
        return false;
    }

    public Person(String name, int age, char gender) {
        this.name = name;
        this.age = age;
        this.gender = gender;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public char getGender() {
        return gender;
    }

    public void setGender(char gender) {
        this.gender = gender;
    }
}
